#include "bullet.h"
#include <cmath>
#define PI 3.14159265
#include "log.h"
#include "settings.h"
#include "game.h"

cBullet::cBullet(int x, int y, int speed, int direction)
{
    m_x = x;
    m_y = y;
    m_speed = speed;
    m_direction = direction;
    m_disabled = false;

    m_hitbox = new Hitbox();
    m_hitbox->x = (int)m_x;
    m_hitbox->y = (int)m_y;
    m_hitbox->rad = 8;

    m_texture.loadTextureFromFile("img/bullet.png");
}

cBullet::~cBullet()
{
    //m_texture.freeTexture();
}

void cBullet::moveBullet(int frameTime)
{
    m_x += m_speed * cos(m_direction * PI / 180) * frameTime / 1000.f;
    m_y += m_speed * sin(m_direction * PI / 180) * frameTime / 1000.f;

    m_hitbox->x = (int)m_x;
    m_hitbox->y = (int)m_y;

    if ((m_x < -10) || (m_y < -10) || (m_x > getInt("WINDOW_WIDTH") + 10) || (m_y > getInt("WINDOW_HEIGHT") + 10))
        //cGame::instance().currentMap()->destroyBullet(this);
        disable();
}

cTexture* cBullet::texture()
{
    return &m_texture;
}

Hitbox* cBullet::hitbox()
{
    return m_hitbox;
}

void cBullet::update(int frameTime)
{
    moveBullet(frameTime);
    //if (m_y < 110 && m_y > 100)
    //    cGame::instance().currentMap()->createBullet(10, 10, 10, 10);
}

void cBullet::disable()
{
    m_disabled = true;
    m_texture.freeTexture();
}

bool cBullet::disabled()
{
    return m_disabled;
}

int cBullet::x()
{
    return (int)m_x;
}

int cBullet::y()
{
    return (int)m_y;
}
